package com.castle.fortress.admin.cms.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.castle.fortress.admin.cms.dto.CmsLinkDto;
import com.castle.fortress.admin.cms.entity.CmsLinkEntity;
import com.castle.fortress.admin.cms.mapper.CmsLinkMapper;
import com.castle.fortress.admin.cms.service.CmsLinkService;
import com.castle.fortress.common.utils.ConvertUtil;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * cms友链管理表 服务实现类
 *
 * @author castle
 * @since 2022-02-24
 */
@Service
public class CmsLinkServiceImpl extends ServiceImpl<CmsLinkMapper, CmsLinkEntity> implements CmsLinkService {
    /**
    * 获取查询条件
    * @param cmsLinkDto
    * @return
    */
    public QueryWrapper<CmsLinkEntity> getWrapper(CmsLinkDto cmsLinkDto){
        QueryWrapper<CmsLinkEntity> wrapper= new QueryWrapper();
        if(cmsLinkDto != null){
            CmsLinkEntity cmsLinkEntity = ConvertUtil.transformObj(cmsLinkDto,CmsLinkEntity.class);
            wrapper.like(StrUtil.isNotEmpty(cmsLinkEntity.getTitle()),"title",cmsLinkEntity.getTitle());
            wrapper.like(cmsLinkEntity.getCategoryId() != null,"category_id",cmsLinkEntity.getCategoryId());
        }
        return wrapper;
    }


    @Override
    public IPage<CmsLinkDto> pageCmsLink(Page<CmsLinkDto> page, CmsLinkDto cmsLinkDto) {
        QueryWrapper<CmsLinkEntity> wrapper = getWrapper(cmsLinkDto);
        Page<CmsLinkEntity> pageEntity = new Page(page.getCurrent(), page.getSize());
        Page<CmsLinkEntity> cmsLinkPage=baseMapper.selectPage(pageEntity,wrapper);
        Page<CmsLinkDto> pageDto = new Page(cmsLinkPage.getCurrent(), cmsLinkPage.getSize(),cmsLinkPage.getTotal());
        pageDto.setRecords(ConvertUtil.transformObjList(cmsLinkPage.getRecords(),CmsLinkDto.class));
        return pageDto;
    }


    @Override
    public List<CmsLinkDto> listCmsLink(CmsLinkDto cmsLinkDto){
        QueryWrapper<CmsLinkEntity> wrapper = getWrapper(cmsLinkDto);
        List<CmsLinkEntity> list = baseMapper.selectList(wrapper);
        return ConvertUtil.transformObjList(list,CmsLinkDto.class);
    }

    @Override
    public List<CmsLinkDto> cmsTagListCmsLink(Map<String, Object> map) {
        return ConvertUtil.transformObjList(baseMapper.cmsTagListCmsLink(map), CmsLinkDto.class);
    }
}

